//
//  CLOfflineCacheTool.m
//  togetherLBS
//
//  Created by Phyllis Hu on 15/8/13.
//  Copyright (c) 2015年 celi. All rights reserved.
//

#import "CLOfflineCacheTool.h"


@implementation CLOfflineCacheTool

//沙盒路径
#define Faths NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)

//获取离线缓存文件
#define OfflineCachePath  [Faths objectAtIndex:0]

//文件路径
#define FilePath [OfflineCachePath stringByAppendingPathComponent:@"offlineCacheFile.plist"];

/*
 *  保存数据到ContactPerson.plist文件中
 */
+(void) saveContactPersonPlistFileData:(id) data member:(NSString*)memberID cacheType:(NSString*)cacheType{
    BOOL isSave = NO;
    int indexArray = 0;
    
    //检查沙盒中是否存在这个文件
//    NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
//    NSString *plistPath1 = [paths objectAtIndex:0];
//    NSString *filename=[plistPath1 stringByAppendingPathComponent:@"offlineCacheFile.plist"];
    NSString *filename= FilePath;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if(![fileManager fileExistsAtPath:filename]) //如果不存在
    {
        DLog(@"xxx.txt is not exist");
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"offlineCacheFile" ofType:@"plist"]; //获取程序包中相应文件的路径
        NSError *error;
        if([fileManager copyItemAtPath:plistPath toPath:filename error:&error]) //拷贝
        {
            DLog(@"copy xxx.txt success");
        }
        else
        {
            DLog(@"%@",error);
        }
    }
    
    NSMutableArray *datas = [[NSMutableArray alloc] initWithContentsOfFile:filename];
    for (NSDictionary *dict in datas) {
        if([memberID isEqualToString:[dict valueForKey:@"deviceID"]]&&
           [cacheType isEqualToString:[dict valueForKey:@"cacheType"]]){
            isSave = YES;
            break;
        }
        indexArray++;
    }
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:memberID forKey:@"deviceID"];
    [dict setValue:data forKey: @"data"];
    [dict setValue:cacheType forKey:@"cacheType"];
    
    if(isSave == NO){ //文件中没有保存
        [datas addObject:dict];
    }else{  //文件中有保存
        [datas replaceObjectAtIndex:indexArray withObject:dict];
    }
    //输入写入
    BOOL  flg = NO;
    flg = [datas writeToFile:filename atomically:YES];
    if (flg) {
        DLog(@"写入成功----");
    }else{
        DLog(@"写入失败----");
    }
    
}


/*
 * 从ContactPerson.plist文件中读取颜色值
 */
+ (id)readColorsPlistFileMember:(NSString*)memberID cacheType:(NSString*)cacheType{
    
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *plistPath = [paths objectAtIndex:0];
    NSString *filename=[plistPath stringByAppendingPathComponent:@"offlineCacheFile.plist"];
    
    NSArray *datas = [[NSArray alloc] initWithContentsOfFile:filename];
    
    NSArray *data = nil;
    
    for(NSDictionary *dict in datas){
        if(([memberID isEqualToString:[dict valueForKey:@"deviceID"]])&&([cacheType isEqualToString:[dict valueForKey:@"cacheType"]])){
            data = [dict valueForKey:@"data"];
            break;
        }
    }
    return data;
}

@end
